unit uRefugoApont;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uPai_new, Mask, JvExMask, JvToolEdit, JvBaseEdits, StdCtrls,
  Menus, DB, Buttons, ExtCtrls, MMJPanel, FMTBcd, SqlExpr, DBClient,
  Provider, Grids, DBGrids, JvExDBGrids, JvDBGrid, DBXpress;

type
  TfRefugoApont = class(TfPai_new)
    sqlBusca: TSQLQuery;
    gridOf: TJvDBGrid;
    RefOf: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    RefMotivo: TEdit;
    Motivo: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    RefSeq: TEdit;
    RefQtde: TJvCalcEdit;
    edProduto: TEdit;
    edMotivo: TEdit;
    edSeq: TEdit;
    edOf: TEdit;
    Label3: TLabel;
    sqlRef: TSQLDataSet;
    dspRef: TDataSetProvider;
    cdsRef: TClientDataSet;
    dsRefugo: TDataSource;
    edProdOf: TEdit;
    RefProd: TComboBox;
    sqlP: TSQLDataSet;
    dspP: TDataSetProvider;
    cdsP: TClientDataSet;
    Label6: TLabel;
    DtaApont: TJvDateEdit;
    procedure btnGravarClick(Sender: TObject);
    procedure RefOfExit(Sender: TObject);
    procedure RefProdExit(Sender: TObject);
    procedure RefMotivoExit(Sender: TObject);
    procedure RefSeqExit(Sender: TObject);
    procedure RefProdClick(Sender: TObject);
  private
    function busca(sql: String): String;
    procedure buscaRefugo;
    { Private declarations }
  public
    codpro: string;
    { Public declarations }
  end;

var
  fRefugoApont: TfRefugoApont;

implementation

uses udm;

{$R *.dfm}

procedure TfRefugoApont.btnGravarClick(Sender: TObject);
var sMensagem, str : String;
 Save_Cursor:TCursor;
 TDD: TTransactionDesc;
begin
 if (DtaApont.Text = '  /  /    ') then
 begin
   MessageDlg('Informe a data do lancamento.', mtWarning, [mbOK],0);
   exit;
 end;

  TDD.TransactionID := 1;
  TDD.GlobalID := 0;
  TDD.IsolationLevel := xilREADCOMMITTED;

  // Refugo de Materiais
 { Save_Cursor := Screen.Cursor;
  try
    Screen.Cursor := crHourGlass;}
  {if (dm.sProcRefugo.Active) then
    dm.sProcRefugo.Close;
  dm.sProcRefugo.ParamByName('COFDET1').AsString  := RefOf.Text;
  dm.sProcRefugo.ParamByName('CODPRO').AsString  := RefProd.Text;
  dm.sProcRefugo.ParamByName('QTDREF1').AsString  := refQtde.Text;
  dm.sProcRefugo.ParamByName('MOTREF1').AsString  := RefMotivo.Text;
  dm.sProcRefugo.ParamByName('USUARIO').AsString := dm.user;
  dm.sProcRefugo.ParamByName('CODSEQ').AsString  := refSeq.Text;
  dm.sProcRefugo.ParamByName('COLETOR').AsString := 'TERMINAL';}
  str := 'INSERT INTO TMP_ATS_REFUGO(COD_OFDET, COD_PRO, QTDE, MOTIVO, COLETOR,'+
    ' USUARIO, CODSEQ, DTAAPONT, TIPO) VALUES( ' +
    RefOf.Text + ', ' + QuotedStr(RefProd.Text) + ', ' + refQtde.Text + ', ' +
    RefMotivo.Text + ', ' + QuotedStr('COLETOR') + ', ' + QuotedStr(dm.user) + ', ' +
    QuotedStr(refSeq.Text) + ', to_date(' + QuotedStr(FormatDateTime('dd/mm/yyyy', DtaApont.Date)) +
    ', '+ QuotedStr('dd/mm/yyyy') +  '), ' +
    QuotedStr('REFUGO') + ')';
  //Try
  //  Screen.Cursor :=crHourglass; // ampulheta

  Try
    //dm.sProcRefugo.ExecProc;
    dm.SQLConnection1.StartTransaction(TDD);
    dm.SQLConnection1.ExecuteDirect(str);
    dm.SQLConnection1.Commit(TDD);
    buscaRefugo;
    dm.SQLConnection1.StartTransaction(TDD);
    dm.SQLConnection1.ExecuteDirect('DELETE FROM TMP_ATS_REFUGO WHERE USUARIO = ' +
      QuotedStr(dm.user));
    dm.SQLConnection1.Commit(TDD);
    Screen.Cursor := crDefault; // Normal
    MessageDlg('Apontado com sucesso.', mtInformation, [mbOK],0);
    RefOf.SetFocus;
  except
    on E : EDatabaseError do
    begin
      ShowMessage('Classe: ' + e.ClassName + chr(13) + 'Mensagem: ' + e.Message);
      dm.SQLConnection1.Rollback(TDD); //on failure, undo the changes}
    end;      {    exit;
      end
      else begin
        MessageDlg('ERRO REFUGO', mtWarning, [mbOK],0);
        exit;
      end;}
  end;

  //finally
  //  Screen.Cursor := Save_Cursor;
  //end;


end;

function TfRefugoApont.busca(sql: String): String;
begin
  if (sqlBusca.Active) then
    sqlBusca.Close;
  sqlBusca.SQL.Clear;
  sqlBusca.SQL.Add(sql);
  sqlBusca.Open;
  result := sqlBusca.Fields[0].AsString;
end;

procedure TfRefugoApont.RefOfExit(Sender: TObject);
var str1: String;
begin
  if (RefOf.Text <> '') then
  begin
    edOf.Text := busca('SELECT DOF_CODIOF FROM ATS_OF, ATS_DETOF ' +
      ' WHERE DOF_CODIOF = COF_CODIOF AND DOF_CODIGO = ' +
      RefOf.Text);
    codpro := busca('SELECT COF_CODPRO FROM ATS_OF, ATS_DETOF ' +
      ' WHERE DOF_CODIOF = COF_CODIOF AND DOF_CODIGO = ' +
      RefOf.Text);
    edProdOf.Text := busca('SELECT COF_CODPRO ||' + QuotedStr('-') + ' || PRO_DESCRI ' +
      ' FROM ATS_OF, ATS_DETOF, F_PRODS ' +
      ' WHERE DOF_CODIOF = COF_CODIOF AND COF_CODPRO = PRO_CODPRO ' +
      ' AND DOF_CODIGO = ' +
      RefOf.Text);

    if (cdsP.Active) then
      cdsP.Close;
    str1 := 'SELECT COF_CODPRO COD_PRO ' +
            '  FROM ATS_OF, ATS_DETOF ' +
            ' WHERE COF_CODIOF = DOF_CODIOF '+
            '   AND DOF_CODIGO = ' + RefOf.Text +
            ' UNION ALL ' +
            'SELECT OFC_CODPRO COD_PRO ' +
            '  FROM ATS_OFCONS WHERE OFC_COFDET = ' + RefOf.Text;
    cdsP.CommandText := str1;
    cdsP.Open;
    RefProd.Items.Clear;
    while not cdsP.Eof do
    begin
      RefProd.Items.Add(cdsP.Fields[0].AsString);
      cdsP.Next;
    end;
  end;  
end;

procedure TfRefugoApont.RefProdExit(Sender: TObject);
begin
  if (RefProd.Text <> '') then
    edProduto.Text := busca('SELECT PRO_DESCRI FROM F_PRODS WHERE PRO_CODPRO = ' +
      QuotedStr(RefProd.Text));
  buscaRefugo;      
end;

procedure TfRefugoApont.RefMotivoExit(Sender: TObject);
begin
  if (RefMotivo.Text <> '') then
    edMotivo.Text := busca('SELECT TBL_DESCRI FROM F_TABELAS WHERE TBL_CODTAB = ' +
      RefMotivo.Text);
end;

procedure TfRefugoApont.RefSeqExit(Sender: TObject);
begin
  if (RefSeq.Text <> '') then
    edSeq.Text := busca('SELECT ROF_HISTOR FROM F_ROTOF WHERE ROF_CODSEQ = ' +
      QuotedStr(RefSeq.Text));
end;

procedure TfRefugoApont.buscaRefugo;
var tipo, sql: string;
begin
  tipo := '';
  tipo := busca('SELECT PRO_CODSUC FROM F_PRODS WHERE  PRO_CODPRO = ' +
    QuotedStr(refProd.Text));
  if (tipo = '') then // Refugo
  begin
    sql := 'SELECT RFG_DT_RFG DATA, RFG_QT_RFG QUANTIDADE, RFG_CODREF MOTIVO, ' +
      'RFG_LOTMAT LOTE FROM F_REFUGO WHERE RFG_CODIOF = ' +
      QuotedStr(edOf.Text);
  end
  else begin
    sql := 'SELECT OFS_DTLANC DATA, OFS_QTDPRO QUANTIDADE, OFS_CODMOT MOTIVO, ' +
      'OFS_LOTMAT LOTE FROM F_OFSUCATA WHERE OFS_CODIOF = ' +
      QuotedStr(edOf.Text);
  end;
  if (cdsRef.Active) then
    cdsRef.Close;
  cdsRef.CommandText := sql;
  cdsRef.Open;
end;

procedure TfRefugoApont.RefProdClick(Sender: TObject);
begin
  inherited;
  if (RefProd.Text <> '') then
    edProduto.Text := busca('SELECT PRO_DESCRI FROM F_PRODS WHERE PRO_CODPRO = ' +
      QuotedStr(RefProd.Text));

end;

end.
